package meijuli.com.join51.business.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.RechargeActivity;
import meijuli.com.join51.business.activity.WebViewActivity;
import meijuli.com.join51.business.adapter.PayPriceAdapter;
import meijuli.com.join51.business.adapter.PayTypeAdapter;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.PayPrice;
import meijuli.com.join51.business.model.PayResult;
import meijuli.com.join51.business.model.PayType;
import meijuli.com.join51.business.model.PayTypeObj;
import meijuli.com.join51.business.service.ApiService;
import meijuli.com.join51.business.ui.widget.QrPayFragment;
import meijuli.com.join51.business.utils.GoInternalAppUtil;
import meijuli.com.join51.business.utils.SoftInputUtil;
import com.jhx.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;

/**
 * 预购
 * @author wenqin 2017-07-12 18:02
 */

public class RechargeFragment extends BaseFragment {
    RecyclerView mRechargeRv;
    RecyclerView mPaymentRv;

    private List<PayPrice> mPayPrices;
    private List<PayType> mPayTypes;
    private PayTypeAdapter mPayTypeAdapter;
    private PayPriceAdapter mPayPriceAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recharge, container, false);
        mRechargeRv = view.findViewById(R.id.recharge_rv);
        mPaymentRv = view.findViewById(R.id.payment_rv);
        mRechargeRv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRechargeBtnClick();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mRechargeRv.setNestedScrollingEnabled(false);
        mRechargeRv.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
        mPayPrices = new ArrayList<>();
        mPayPriceAdapter = new PayPriceAdapter(mActivity, mPayPrices);
        mRechargeRv.setAdapter(mPayPriceAdapter);

        mPaymentRv.setNestedScrollingEnabled(false);
        mPaymentRv.setLayoutManager(new LinearLayoutManager(mActivity));
        mPayTypes = new ArrayList<>();
        mPayTypeAdapter = new PayTypeAdapter(mActivity, mPayTypes);
        mPaymentRv.setAdapter(mPayTypeAdapter);

        requestPayments();
        requestPayPrices();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((RechargeActivity) mActivity).setToolBarTitle("预购");
            SoftInputUtil.get().hideSoftKeyboard(mActivity);
        }
    }

    private void requestPayPrices() {
        showLoadingDialog();
        Call<HttpResponse<List<PayPrice>>> call = ApiManager.get().getApiService(ApiService.class).getPayPriceList();
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback<List<PayPrice>>() {
            @Override
            public void onSuccess(String url, List<PayPrice> data) {
                dismissDialog();
                mPayPrices.clear();
                mPayPrices.addAll(data);
                mPayPriceAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
            }
        });
    }

    private void requestPayments() {
        showLoadingDialog();
        Call<HttpResponse<PayTypeObj>> call = ApiManager.get().getApiService(ApiService.class).getPaymentList();
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback<PayTypeObj>() {
            @Override
            public void onSuccess(String url, PayTypeObj data) {
                dismissDialog();
                mPayTypes.clear();
                mPayTypes.addAll(data.getCashPay());
                mPayTypeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
            }
        });
    }

    void onRechargeBtnClick() {
        if (mPayTypes.isEmpty() || mPayPrices.isEmpty()) {
            ToastUtil.getInstance().showToast("获取数据异常，请稍后重试");
            return;
        }

        showLoadingDialog();
        Call<HttpResponse<PayResult>> call = ApiManager.get().getApiService(ApiService.class).getPayResult(
                mPayPriceAdapter.getSelectedPayPrice().getDmId(),
                mPayTypeAdapter.getSelectedPayType().getPayType());
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback<PayResult>() {
            @Override
            public void onSuccess(String url, PayResult data) {
                dismissDialog();
                toRecharge(data);
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
            }
        });
    }

    private void toRecharge(PayResult payResult) {
        switch (mPayTypeAdapter.getSelectedPayType().getPayType()) {
            case PayType.TYPE_ALI_PAY:
                GoInternalAppUtil.toBrowser(mActivity, payResult.getCodeUrl());
                break;
            case PayType.TYPE_QUICK_PAY:
                WebViewActivity.start(mActivity, payResult.getCodeUrl());
                break;
            default:
                toQrPayFragment(payResult, mPayTypeAdapter.getSelectedPayType().getPayType());
                break;
        }
    }

    private void toQrPayFragment(final PayResult payResult, int payType) {
        QrPayFragment qrPayFragment = QrPayFragment.newInstance(payResult, payType);
        switchFragment(this, qrPayFragment);
    }

}
